package gg.zsw.dataframework.decode;

import java.util.ArrayList;
import java.util.List;

/**
 * 强约束，类型不匹配的话都会直接崩
 *
 * @author zhou.sw
 * @Date 2020/6/29 10:58
 */
public class DecodeChain<Input, Output> {

    private List<DecodeHandler> list = new ArrayList<>();

    public void addDecodeHandler(DecodeHandler decodeHandler) {
        list.add(decodeHandler);
    }

    /**
     * 要保证input不为null
     *
     * @param input
     * @return
     */
    public Output handle(Input input) {
        // 类型转换，以便顺畅进行链式调用
        Object in = input;
        // 链式调用
        for (DecodeHandler decodeHandler : list) {
            in = decodeHandler.decodeObject(in);
            if (in == null) {
                return null;
            }
        }
        return (Output) in;
    }

}
